<form lay-filter="addProductionPlanFormDiv" class="layui-form open-form">
  <div class="open-model" id="addProductionPlanFormContent">
    <div class="open-auto">
      <div>
        制造令信息
        <table class="layui-table" id="projectInfoTable" lay-filter="projectInfoTable"></table>
      </div>
      <div class="layui-tab layui-tab-brief" lay-filter="test-hash">
        <ul class="layui-tab-title">
          <li class="layui-this" lay-id="11">MRP齐套</li>
          <li lay-id="22">工序工艺</li>
        </ul>
        <div class="layui-tab-content">
          <div class="layui-tab-item layui-show">
            <table class="layui-table" id="mrpTable" lay-filter="mrpTable"></table>
          </div>
          <div class="layui-tab-item">
            <table class="layui-table" id="processTable" lay-filter="processTable"></table>
          </div>
        </div>
      </div>
    </div>
  </div>
</form>
<script>
  layui.use(["form", "table", "admin", "jquery", "laydate", "treetable"], async function () {
    let form = layui.form;
    let table = layui.table;
    let admin = layui.admin;
    let $ = layui.jquery;
    var treetable = layui.treetable;
    let laydate = layui.laydate;
    const nowData = { ...parentData };
    let data = nowData.data;
    table.render({
      elem: "#projectInfoTable",
      data: data ? [data] : [],
      cols: [
        [
          {
            field: "productionPlanRecordCode",
            title: "制造令编号",
            align: "center",
            width: 160
          },
          { field: "planTypeName", title: "生产计划类型", align: "center" },
          { field: "bookCode", title: "基准书code", align: "center" },
          { field: "orderTypeDesc", title: "计划订单类型", align: "center" },
          // { field: "productTypeName", title: "物料类别", align: "center" },
          { field: "customerSkuCode", title: "物料编码", align: "center" },
          { field: "customerSkuSpec", title: "物料规格", align: "center" },
          { field: "productionPlanQuantity", title: "生产数量", align: "center" },
          {
            field: "shipmentTime",
            title: "交货时间",
            align: "center",
            width: 160
          },
          { field: "statusDesc", title: "状态", align: "center" },
          {
            field: "createTime",
            title: "创建时间",
            align: "center",
            width: 160
          }
        ]
      ]
    });

    table.render({
      elem: "#processTable",
      data: [],
      cols: [
        [
          { type: "numbers", title: "序号" },
          {
            field: "name",
            title: "工序名称"
          },
          {
            field: "processNumber",
            title: "工序号"
          },
          {
            field: "code",
            title: "工序编码"
          },

          {
            field: "type",
            title: "工序属性",
            templet: admin.setTableSelect(
              "type",
              [
                { value: 0, label: "首工序" },
                { value: 1, label: "过程工序" },
                { value: 2, label: "末工序" }
              ],
              true,
              "type"
            )
          },
          {
            field: "content",
            title: "工序工艺要求",
            width: 200
          },
          {
            field: "hoursQuota",
            title: "工时定额(分钟)"
          }

          //   {
          //     toolbar: "#productProjectTableBar",
          //     title: "操作",
          //   }
        ]
      ]
    });
    let treetableParms = {
      id: "#mrpTable",
      elem: "#mrpTable",
      data: [],
      treeIdName: "productDetailId", // id的字段名
      treePidName: "parentId",
      treeColIndex: 1, //层级关系展示在第几列
      treeSpid: "-1", //是否有最高父id没有-1表示
      page: false,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers", title: "序号", fixed: "left" },
          { field: "productCode", title: "子件料号", align: "center", width: 200 },
          { field: "productName", title: "子件名称", align: "center" },
          { field: "productSpec", title: "子件规格", align: "center" },
          { field: "productTypeName", title: "类别", align: "center" },
          //   { field: "productAttrName", title: "性质", align: "center" },
          //   { field: "skuQuantity", title: "属性", align: "center" },
          { field: "inUnitCount", title: "单位需求数", align: "center" },
          {
            field: "allQuantity",
            title: "总需求数",
            align: "center"
          },
          { field: "haveQuantity", title: "库存数", align: "center" },
          {
            title: "是否缺料",
            align: "center",
            templet: function (d) {
              if (d.LAY_INDEX == 1) {
                return "不缺料";
              } else {
                return d.haveQuantity < d.allQuantity ? '<div style="color:red">缺料</div>' : '<div style="color:green">不缺料</div>';
              }
            }
          },
          {
            title: "缺料数",
            align: "center",
            templet: function (d) {
              
              if (d.LAY_INDEX == 1) {
                return "不缺料";
              } else {
                return d.allQuantity - d.haveQuantity < 0 ? "不缺料" : d.allQuantity - d.haveQuantity;
              }
            }
          },
          {
            title: "操作",
            align: "center",
            width: 200,
            fixed: "right",
            templet: function (d) {
              return `
                <a class="layui-btn layui-btn-xs" lay-event="addPurchasePlan">采购需求</a>
              <a class="layui-btn layui-btn-xs" lay-event="addProductionPlan">生产预计划</a>`;
            }
          }
        ]
      ]
    };
    table.on("tool(mrpTable)", function (obj) {
      if (obj.event === "addProductionPlan") {
        admin.formOpen({
          type: 1,
          area: ["85%", "80%"],
          title: "添加生产计划",
          path: "components/productionPlan/productionPlanForm.html",
          success: function () {
            parentData.eventName = "add";
            parentData.planSourceType = "single";
            parentData.productDetailId = obj.data.productDetailId;
          }
        });
      } else if (obj.event === "addPurchasePlan") {
        admin.formOpen({
          title: "添加采购需求",
          area: ["90%", "80%"],
          offset: "50px",
          path: "components/purchase/purchaseOrderForm.html",
          success: function () {
            parentData.eventName = "addPlan";
            parentData.productDetailId = obj.data.productDetailId;
          }
        });
      }
    });

    // treetable.render({ ...treetableParms });
    const bookData = await admin.reqPro("enterprise/benchmarkBook/admin/benchmarkBook/detail/" + data.bookId);
    const processList = bookData.data.echnologyList;
    table.reload("processTable", { data: processList });
    // const bomList = bookData.data.bomList;
    const bomRes = await admin.reqPro("enterprise/benchmarkBook/admin/benchmarkBook/getBomListBy/" + data.bookId);
    const bomList = bomRes.data;
    console.log(123, treetableParms);
    const formattedBomList = bomList.map((item) => ({
      ...item,
      parentId: String(item.parentId),
      productDetailId: String(item.productDetailId),
      hasChild: 1,
      hasEnableChild: 1,
      id: item.productDetailId,
      allQuantity: item.inUnitCount * table.cache["projectInfoTable"][0].productionPlanQuantity
    }));
    treetableParms.data = formattedBomList;
    console.log(123, treetableParms);
    treetable.reload({ ...treetableParms });
  });
</script>
